package com.example.administrator.szb.fragments.fragment_forWD;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.fragments.base.BaseFragment;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by D N on 2017/9/20.
 */

public class FragmentWDType extends BaseFragment {
    View view;


    @Bind(R.id.wd_rmwt)
    TextView wdRmwt;
    @Bind(R.id.wd_zxwt)
    TextView wdZxwt;
    @Bind(R.id.wd_dhdwt)
    TextView wdDhdwt;
    private FragmentManager fragmentManager;
    private ArrayList<Fragment> fragmentArrayList;
    private FragmentWDWTType fragment_rmwt,fragment_zxwt,fragment_dhdwt;
    private FragmentTransaction fragmentTransaction;
    Bundle bundle;


    public static FragmentWDType newInstance(int business_id){
        Bundle bundle = new Bundle();
        bundle.putInt("text",business_id);
        FragmentWDType fragmentWDType = new FragmentWDType();
        fragmentWDType.setArguments(bundle);
        return  fragmentWDType;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.message_fragment,container,false);
        ButterKnife.bind(this, view);
        bundle = new Bundle();//初始化bundle 用于向FragmentWDWTType传值
        bundle.putInt("business_id",getArguments().getInt("text"));
        initFragment();//开启问题类型的切换
        return view;
    }

    private void initFragment() {
        fragmentManager = getChildFragmentManager();
        fragmentArrayList = new ArrayList<Fragment>();
        changeStatu(0);
    }


    /**
     * 当这个fragment可见的时候才开始加载数据
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }


    /**
     * 点击事件
     *由入参按照约定加密算法加密生成code，截取前八位字符串生成code参数
     id 用户id(未注册时为0)
     business_id 全部业务0具体业务是具体业务的id
     type 1热门2最新3待回答(必填)第一次请求4表示全部请求
     current_page 1首次请求从1开始，上拉加载时根据我返回模块的最大页码+1为请求参数页码(必填)
     per_page 每页显示数量默认10（选填）

     * @param view
     */
    @OnClick({R.id.wd_rmwt, R.id.wd_zxwt, R.id.wd_dhdwt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wd_rmwt:
                changeStatu(0);
                break;
            case R.id.wd_zxwt:
                changeStatu(1);
                break;
            case R.id.wd_dhdwt:
                changeStatu(2);
                break;
        }
    }

    /**
     * 根据点击问题的不同类型修改字体颜色
     *
     * @param i
     */
    private void changeStatu(int i) {
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        switch (i) {
            case 0:
                if(fragment_rmwt == null){
                    fragment_rmwt = new FragmentWDWTType();
                    bundle.putInt("type",1);
                    fragment_rmwt.setArguments(bundle);
                    changeFragment(fragment_rmwt);
                }else {
                    fragmentTransaction.show(fragment_rmwt);
                }
                wdRmwt.setTextColor(getResources().getColor(R.color.toolbar_home));
                wdZxwt.setTextColor(getResources().getColor(R.color.cancel));
                wdDhdwt.setTextColor(getResources().getColor(R.color.cancel));

                break;
            case 1:
                if(fragment_zxwt == null){
                    fragment_zxwt = new FragmentWDWTType();
                    bundle.putInt("type",2);
                    fragment_zxwt.setArguments(bundle);
                    changeFragment(fragment_zxwt);
                }else {
                    fragmentTransaction.show(fragment_zxwt);
                }
                wdZxwt.setTextColor(getResources().getColor(R.color.toolbar_home));
                wdRmwt.setTextColor(getResources().getColor(R.color.cancel));
                wdDhdwt.setTextColor(getResources().getColor(R.color.cancel));
                break;
            case 2:
                if(fragment_dhdwt == null){
                    fragment_dhdwt = new FragmentWDWTType();
                    bundle.putInt("type",3);
                    fragment_dhdwt.setArguments(bundle);
                    changeFragment(fragment_dhdwt);
                }else {
                    fragmentTransaction.show(fragment_dhdwt);
                }
                wdDhdwt.setTextColor(getResources().getColor(R.color.toolbar_home));
                wdZxwt.setTextColor(getResources().getColor(R.color.cancel));
                wdRmwt.setTextColor(getResources().getColor(R.color.cancel));
                break;
        }

        fragmentTransaction.commit();
    }

    private void changeFragment(FragmentWDWTType fragment) {
        fragmentArrayList.add(fragment);
        fragmentTransaction.add(R.id.wd_content, fragment);
        fragmentTransaction.show(fragment);
    }

    private void hideFragments(FragmentTransaction fragmentTransaction) {
        for (Fragment fragment : fragmentArrayList) {
            fragmentTransaction.hide(fragment);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


}
